version 8.2
capture clear
capture log close
set more off
set mem 1000m
set mat 800


use "panel_data_clean.dta", replace

*browse countryname year h7a
codebook h7a
count
tab h7a

*replace variables by missing if negative number:
local list_variables h7a n2b n2c n2d n2e n2f n2g n2h n2j n2i d2 l1 d1a2
foreach var of local list_variables{
	replace `var'=. if `var'<0
	}


*browse d1a2
codebook d1a2

****Value added per worker:
*browse n2*			
*browse n2b n2c n2d n2e n2f n2g n2h n2j n2i
egen intermediate_costs=rowtotal(n2b n2c n2d n2e n2f n2g n2h n2j n2i)
gen va_perwo=(d2-intermediate_costs)/l1
label variable va_perwo "Value added per worker"


*browse va_perwo

***h7a
*browse h7a
tab h7a, missing
replace h7a=. if h7a==0|h7a>=5


***generate a continent variable:

gen continent="Europe" if countryname=="Albania"|countryname=="Armenia"|countryname=="Azerbaijan"|countryname=="Belarus" ///
|countryname=="Bosnia and Herzegovina"|countryname=="Bulgaria"|countryname=="Croatia"|countryname=="Czech Republic"|countryname=="Estonia" ///
|countryname=="Georgia"|countryname=="Hungary"|countryname=="Kosovo"|countryname=="Latvia"|countryname=="Lithuania"|countryname=="Macedonia, FYR" ///
|countryname=="Moldova"|countryname=="Montenegro"|countryname=="Poland"|countryname=="Romania"|countryname=="Russian Federation"|countryname=="Serbia" ///
|countryname=="Slovak Republic"|countryname=="Slovenia"|countryname=="Sweden"|countryname=="Ukraine"

*East Asia and pacific and South Asia and central asia:
replace continent="Asia" if countryname=="Afghanistan" ///
|countryname=="Bangladesh"|countryname=="Bhutan"|countryname=="Cambodia"|countryname=="China"|countryname=="Fiji"|countryname=="India" ///
|countryname=="Indonesia"|countryname=="Kazakhstan"|countryname=="Kyrgyz Republic"|countryname=="Lao PDR"|countryname=="Malaysia"|countryname=="Micronesia, Fed. Sts." ///
|countryname=="Mongolia"|countryname=="Myanmar"|countryname=="Nepal"|countryname=="Pakistan"|countryname=="Papua New Guinea"|countryname=="Philippines" ///
|countryname=="Samoa"|countryname=="Solomon Islands"|countryname=="Sri Lanka"|countryname=="Tajikistan"|countryname=="Thailand"|countryname=="Timor-Leste" ///
|countryname=="Tonga"|countryname=="Trinidad and Tobago"|countryname=="Uzbekistan"|countryname=="Vanuatu"|countryname=="Vietnam"

*middle east and North Africa:
replace continent="Middle east" if countryname=="Egypt, Arab Rep." ///
|countryname=="Iraq"|countryname=="Israel"|countryname=="Jordan"|countryname=="Lebanon"|countryname=="Morocco"|countryname=="Tunisia" ///
|countryname=="Turkey"|countryname=="West Bank and Gaza"|countryname=="Yemen"

*Latin america, Central America, the Carribean:
replace continent="Latin America" if countryname=="Antigua and Barbuda"|countryname=="Argentina" ///
|countryname=="Bahamas, The"|countryname=="Barbados"|countryname=="Belize"|countryname=="Bolivia"|countryname=="Brazil" ///
|countryname=="Chile"|countryname=="Colombia"|countryname=="Costa Rica"|countryname=="Dominica"|countryname=="Dominican Republic"|countryname=="Ecuador" ///
|countryname=="El Salvador"|countryname=="Guyana"|countryname=="Grenada"|countryname=="Guatemala"|countryname=="Honduras"|countryname=="Jamaica"|countryname=="Mexico"|countryname=="Nicaragua" ///
|countryname=="Panama"|countryname=="Paraguay"|countryname=="Peru"|countryname=="St. Kitts and Nevis"|countryname=="St. Lucia"|countryname=="St. Vincent and the Grenadines" ///
|countryname=="Suriname"|countryname=="Uruguay"|countryname=="Venezuela, RB"

*Sub-Saharan Africa:
replace continent="Africa" if countryname=="Angola" ////
|countryname=="Benin"|countryname=="Botswana"|countryname=="Burkina Faso"|countryname=="Burundi"|countryname=="Cabo Verde" ///
|countryname=="Cameroon"|countryname=="Central African Republic"|countryname=="Chad"|countryname=="Congo, Dem. Rep."|countryname=="Congo, Rep." ///
|countryname=="Cote d'Ivoire"|countryname=="Djibouti"|countryname=="Eritrea"|countryname=="Ethiopia"|countryname=="Gabon"|countryname=="Gambia, The" ///
|countryname=="Ghana"|countryname=="Guinea"|countryname=="Guinea-Bissau"|countryname=="Kenya" ///
|countryname=="Lesotho"|countryname=="Liberia"|countryname=="Madagascar"|countryname=="Malawi"|countryname=="Mali"|countryname=="Mauritania" ///
|countryname=="Mauritius"|countryname=="Mozambique"|countryname=="Namibia"|countryname=="Niger"|countryname=="Nigeria"|countryname=="Rwanda" ///
|countryname=="Senegal"|countryname=="Sierra Leone"|countryname=="South Africa"|countryname=="South Sudan"|countryname=="Sudan"|countryname=="Swaziland" ///
|countryname=="Tanzania"|countryname=="Togo"|countryname=="Uganda"|countryname=="Zambia"|countryname=="Zimbabwe"


keep countryname year h7a va_perwo l1 d1a2 continent

save "panel_data_clean2.dta", replace

***prepare a dataset with one observation for country-year: 
use "panel_data_clean2.dta"
collapse (mean) h7a va_perwo, by(countryname year)
sort countryname year
*browse
save "panel_data_clean3.dta", replace

exit


